package com.buba.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.pojo.user.User;
import com.buba.pojo.appointment.Appointment;
import com.buba.pojo.other.DivisionType;
import com.buba.pojo.other.Drugs;
import com.buba.pojo.other.MedicalRecords;
import com.buba.pojo.other.SetMeal;
import com.buba.pojo.record.*;
import com.buba.pojo.user.Campus;
import com.buba.pojo.user.Doctor;
import com.buba.pojo.user.ThePatient;
import com.buba.user.mapper.UserMapper;
import com.buba.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.utils.AliOss;
import com.buba.utils.JwtUtils;
import com.buba.utils.MD5;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.buba.user.vo.AppointmentVo;
import com.buba.user.vo.MedicalAppointmentVo;
import com.buba.user.vo.NucleicAcidVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-06-09
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<AppointmentVo> getAllappointment(String doctorName,String name,Date createTime,Date createTime1,String divisionName) {
        return userMapper.getAllappointment(doctorName,name,createTime,createTime1,divisionName);
    }

    @Override
    public AppointmentVo getByIdappointment(Integer id) {
        return userMapper.getByIdappointment(id);
    }


    @Override
    public Doctor getByIdDoctor(Integer doctorId) {
        return userMapper.getByIdDoctor(doctorId);
    }

    @Override
    public ThePatient getByIdThePatient(Integer patientId) {
        return userMapper.getByIdThePatient(patientId);
    }

    @Override
    public DivisionType getByIdDivisionType(Integer departmentId) {
        return userMapper.getByIdDivisionType(departmentId);
    }


    @Override
    public WaitingRecord getByIdWaitingRecord(Integer houzhenId) {
        return userMapper.getByIdWaitingRecord(houzhenId);
    }

    @Override
    public List<NucleicAcidVo> getAllnucleicAcid(Date createTime, Date createTime1) {
        return userMapper.getAllnucleicAcid(createTime,createTime1);
    }

    @Override
    public NucleicAcidVo getByIdnucleicacid(Integer id) {
        return userMapper.getByIdnucleicacid(id);
    }

    @Override
    public ThePatient getByIdThePatient1(Integer patientId) {
        return userMapper.getByIdThePatient1(patientId);
    }

    @Override
    public WaitingRecord getByIdWaitingRecord1(Integer houzhenId) {
        return userMapper.getByIdWaitingRecord1(houzhenId);
    }

    @Override
    public DivisionType getByIdDivisionType1(Integer departmentId) {
        return userMapper.getByIdDivisionType1(departmentId);
    }

    @Override
    public Campus getByidCampus1(Integer campusId) {
        return userMapper.getByidCampus1(campusId);
    }

    @Override
    public Doctor getByIdDoctor1(Integer doctorId) {
        return userMapper.getByIdDoctor1(doctorId);
    }

    @Override
    public List<MedicalAppointmentVo> getAllmedicalAppointment(Date createTime, Date createTime1) {
        return userMapper.getAllmedicalAppointment(createTime,createTime1);
    }

    @Override
    public MedicalAppointmentVo getByIdmedicalAppointment(Integer id) {
        return userMapper.getByIdmedicalAppointment(id);
    }

    @Override
    public ThePatient getByIdThePatient2(Integer patientId) {
        return userMapper.getByIdThePatient2(patientId);
    }

    @Override
    public SetMeal getBiIdsetMeal(Integer packageId) {
        return userMapper.getBiIdsetMeal(packageId);
    }

    @Override
    public Campus getByidCampus2(Integer campusId) {
        return userMapper.getByidCampus2(campusId);
    }

    @Override
    public DivisionType getByIdDivisionType2(Integer departmentId) {
        return userMapper.getByIdDivisionType2(departmentId);
    }

    @Override
    public PhysicalReport getByIdPhysicalReport(Integer patientId) {
        return userMapper.getByIdPhysicalReport(patientId);
    }

    @Override
    public Convention getAllconvention(Integer patientId) {
        return userMapper.getAllconvention(patientId);
    }

    @Override
    public BloodRoutineExaminatio getAllbloodRoutineExaminatio(Integer patientId) {
        return userMapper.getAllbloodRoutineExaminatio(patientId);
    }

    @Override
    public List<MedicalAppointmentVo> getAllrecord() {
        return userMapper.getAllrecord();
    }

    @Override
    public List<Drugs> getAlldrugs() {
        return userMapper.getAlldrugs();
    }

    @Override
    public Drugs getByIddrugs(Integer id) {
        return userMapper.getByIddrugs(id);
    }


    @Override
    public Campus getByidCampus(Integer campusId) {
        return userMapper.getByidCampus(campusId);
    }



    @Override
    public void insertToken(String token, String loginUser, Date date) {
        userMapper.insertToken(token,loginUser,date);
    }

    @Override
    public User login(String userName) {
       return userMapper.login(userName);
    }


    @Override
    public User login(User user) {
        String userPhone = user.getUserPhone();
        String password = user.getPassword();
        User user1 = baseMapper.selectOne(new QueryWrapper<User>().eq("user_phone", userPhone)
                .eq("password", password));
        //生成token
        String token = JwtUtils.getJwtToken(String.valueOf(user1.getId()), user1.getUserName());
        user1.setToken(token);
        return user1;
    }

//    @Override
//    public User login2(String userName) {
//        // System.out.println("service");
//        return userMapper.login2(userName);
//    }

    @Override
    public int resetPwd(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_phone" , user.getUserPhone());
        User user1 = baseMapper.selectOne(wrapper);
        user1.setPassword(MD5.convertMD5(user.getPassword()));
        int i = baseMapper.updateById(user1);
        return i;
    }

    @Override
    public String uploadFile(MultipartFile file, Integer id) {
        log.info("id::"+id);
        log.info("MultipartFile::"+file);
        AliOss aliOss = new AliOss();
        String url = aliOss.upload(file);
        log.info("url:::"+url);
        //查询到当前用户修改当前用户头像
        User user = baseMapper.selectById(id);
        user.setPortrait(url);
        baseMapper.updateById(user);
        return url;
    }

}
